<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%-- 引入页面头 --%>
<%@ include file="/pageHead.jsp"%>
<form name="form1" id="form1" action="${systemctx}/sample/saveSample.web" method="post">
	<input type="hidden" id="ids" name="ids" value="" />
	<input type="hidden" id="gatheramounts" name="gatheramounts" value="" />
	<div class="pageMain">
		<div class="pageTitle">
			<%=MessageResourcesUtils.getMessage(request, "systemmanage.bascsetting.title")%>
			<span></span>
			<%=MessageResourcesUtils.getMessage(request, "systemmanage.channle") %>
			<span></span>
			<%=MessageResourcesUtils.getMessage(request, "systemmanage.channle.add") %>
		</div>
		<div class="pageBody">
			<div class="form-group">
				<ul> 
					<li>
						<i>*</i>
						<label><%=MessageResourcesUtils.getMessage(request, "systemmanage.channle.column.code")%></label>
						<span>
							<input type="text" id="code" name="code" value="${code}" readonly="readonly" class="validate[required]"/>
						</span>
					</li>
					<li>
						<i>*</i>
						<label><%=MessageResourcesUtils.getMessage(request, "systemmanage.channle.column.name")%></label>
						<span>
							<input type="text" id="name" name="name" class="validate[required]"/>
						</span>
					</li>				
				</ul>	
			</div>		
			<table width="100%">
				<tbody>
					<tr>
						<td>
							<table id="channelinfo_datagrid"></table>
						</td>
					</tr>
				</tbody>
			</table>
        	<div class="button-group">
       			<input type="button" class="btn btn-default"  id="butSave" name="butSave" onclick="channelSave()" value='<%=MessageResourcesUtils.getMessage(request, "common.button.save") %>' />
       			<input type="button" class="btn btn-default"  id="butReturn" name="butReturn" value='<%=MessageResourcesUtils.getMessage(request, "common.button.back") %>' />
			</div>
		</div>
	</div>
</form>
<script type="text/javascript">
	$(document).ready(function(){
		channelinfo();
		
		$('#butReturn').click(function(){
			parent.document.location.reload();
		    $.fn.closeIssWindow();
		});
				
	});	
	
	function channelinfo(){
		var lastIndex ;
		$('#channelinfo_datagrid').datagrid({
			title : '<%=MessageResourcesUtils.getMessage(request,"systemmanage.channle.goodList")%>',
			view: myview,
            emptyMsg: '对不起，没有内容！',
			width:770,
			height:420,
			nowrap:true,
			autoRowHeight:false,
			singleSelect:false,		
			striped:true,
			url:'${systemctx}/systemmanage/channel/queryGoodsInfoDataGrid.json',
			sortName: '',
			sortOrder: '',
			remoteSort:false,
			idField:'id',
			pageNumber:1,//在设置分页属性的时候初始化页码
			pagination:false,//如果为true，则在DataGrid控件底部显示分页工具栏
			rownumbers:true,
			columns:[[
				{field:'ck', checkbox:true},
				{title : '<%=MessageResourcesUtils.getMessage(request,"systemmanage.channle.column.goodsName")%>',field : 'goodsname',width:200,sortable : true,align : 'left'},
		        {title : '<%=MessageResourcesUtils.getMessage(request,"systemmanage.channle.column.costPrice")%>',field : 'costprice',width:100,sortable : true,align : 'right'},
		        {title : '<%=MessageResourcesUtils.getMessage(request,"systemmanage.channle.column.unit")%>',field : 'unit',width:100,sortable : true,align : 'left'},
				{title : '<%=MessageResourcesUtils.getMessage(request,"systemmanage.channle.column.preferentialPrice")%>',field : 'preferentialprice',width:150,sortable : true,align : 'left',
					formatter:function(value,row,index){
						return "<span><input  type=\"text\" id=\"gatheramount"+row.id+"\" value=\""+value+"\" onblur=\"famount('"+row.id+"')\" onfocus=\"fselect('"+row.id+"')\"  style=\"text-align:left;width:140px;\"/><input type=\"hidden\" id=\""+row.id+"\" name=\"gatheramount\" value=\""+value+"\" /></span>";
					}
				}				
			]],
			onLoadSuccess : function(data) {
			}
		});	
	}
	
	//保存
	function channelSave(){		
		var exp = $('#form1').validationEngine('validate');
		
		if(exp==true)
		{
			var rows = $('#channelinfo_datagrid').datagrid('getChecked');
			if(rows.length == 0){
				alert("<%=MessageResourcesUtils.getMessage(request, "systemmanage.channle.alert1") %>");
				return;
			}
			//console.log(rows);
			var ids = "";
			var gatheramounts = "";
			for (var i = 0; i < rows.length; i++) {
				var id = rows[i].id;//获取ID
				var gatheramount = $("#"+id).val();	//获取金额		
				ids +=  id + ",";
				gatheramounts += gatheramount + ",";
			}
			if (ids != '') {
				ids = ids.substring(0,ids.length - 1);
			}
			if (gatheramounts != '') {
				gatheramounts = gatheramounts.substring(0,gatheramounts.length - 1);
			}
			$("#ids").val(ids);
			$("#gatheramounts").val(gatheramounts);
			// 显示进度条
			showSending();
			$.ajax({
				type: 'POST',
			    url: '${systemctx}/systemmanage/channel/validationChannel.json',
			    data: $("#form1").serialize(),
			    dataType: 'json',
			    success: function(data)
			    {
			    	//关闭执行...
					closeSending();
			    	if (data.messagetype == '2')
			    	{
			    		//显示AJAX请求后的提示信息
						$.fn.ajaxmessage(data);
			    	}
			    	else
			    	{
			    		form1.action='${systemctx}/systemmanage/channel/addChannel.web?ids=' + ids+'&gatheramounts='+gatheramounts;
						form1.submit();
			    	}
			    }
			});
		}
		
		<%-- if (confirm("<%=MessageResourcesUtils.getMessage(request, "systemmanage.channle.alert2") %>")) {			
			//正在执行...
			showSending();
			$.ajax({
				type : 'POST',
				url : '${systemctx}/systemmanage/channel/addChannel.json?ids=' + ids+'&gatheramounts='+gatheramounts,
				dataType : 'json',
				success : function(data) {
					//关闭执行...
					closeSending();
					if(data.messagetype == 1){
						//显示AJAX请求后的提示信息
						$.fn.ajaxmessage(data);
					}else{
						$.fn.ajaxmessage(data);
					}
				}
			});
		} --%>
		
	}
	
    function fselect(id){
    	var value = $("#"+id).val();
    	$("#gatheramount"+id).val(parseFloat(value).toFixed(2));
    	$("#gatheramount"+id).select();
    }
    
    function famount(id){
    	var value = $("#gatheramount"+id).val();
    	$("#"+id).val(parseFloat(value).toFixed(2));//将值保留两位小数，赋值给隐藏域
    	//将值保留两位小数并且格式化，赋值给显示域
    	var amount = fmoney(eval(value), 2);
    	$("#gatheramount"+id).val(amount);
     	//$("#gatheramount"+id).css('width', '125');
    	//$("#gatheramount"+id).css('text-align', 'right'); 
    	var part = value.split(".");
		if(part[0].length > 12){
			alert('金额整数位最大支持12位，请重新输入!');
			$("#"+id).val(fmoney(eval(0.00), 2));
			$("#gatheramount"+id).val(fmoney(eval(0.00), 2));
		}
    }
    //格式化金额
    function fmoney(s, n) {   
 	   n = n > 0 && n <= 20 ? n : 2;   
 	   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";   
 	   var l = s.split(".")[0].split("").reverse(),   
 	   r = s.split(".")[1];   
 	   t = "";   
 	   for(i = 0; i < l.length; i ++ ) {   
 	      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");   
 	   }   
 	   return t.split("").reverse().join("") + "." + r;   
 	} 
    
    var myview = $.extend({},$.fn.datagrid.defaults.view,{
        onAfterRender:function(target){
            $.fn.datagrid.defaults.view.onAfterRender.call(this,target);
            var opts = $(target).datagrid('options');
            var vc = $(target).datagrid('getPanel').children('div.datagrid-view');
            vc.children('div.datagrid-empty').remove();
            if (!$(target).datagrid('getRows').length){
                var d = $('<div class="datagrid-empty"></div>').html(opts.emptyMsg || '对不起，没有内容！').appendTo(vc);
                d.css({
                    position:'absolute',
                    left:0,
                    top:50,
                    width:'100%',
                    textAlign:'center'
                });
            }
        }
    });
</SCRIPT> 



<%-- 引入页面尾 --%>
<%@ include file="/pageFloor.jsp"%>